<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\BackstageModel;
use App\Models\CommonModel;
use App\Models\UserModel;
use Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;

// require_once '../app/libary/captcha/Code.class.php';

class OrderController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/order/index';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('guest', ['except' => 'logout']);
    // }

    public function index(){
        // DB::connection()->enableQueryLog(); // 開啟查詢日誌
        if(isset($_GET['keywords'])){
            $keywords = trim($_GET['keywords']);
            // $keywords = '使用';
            $list = DB::table(DB::raw('zzb_order o'))->select(DB::raw('o.id,o.uid,p.name,o.order,o.money,o.status,o.add_time,o.time_end,o.order,u.user_name,u.nickname'))->leftJoin(DB::raw('zzb_product p'), DB::raw('p.id'), '=', DB::raw('o.pid'))->leftJoin(DB::raw('zzb_user u'), DB::raw('u.id'), '=', DB::raw('o.uid'))->where(DB::raw('p.name'),'like','%'.$keywords.'%')->orderBy(DB::raw('o.id'),'desc')->paginate(15);
        }else{
            $list = DB::table(DB::raw('zzb_order o'))->select(DB::raw('o.id,o.uid,p.name,o.order,o.money,o.status,o.add_time,o.time_end,o.order,u.user_name,u.nickname'))->leftJoin(DB::raw('zzb_product p'), DB::raw('p.id'), '=', DB::raw('o.pid'))->leftJoin(DB::raw('zzb_user u'), DB::raw('u.id'), '=', DB::raw('o.uid'))->orderBy(DB::raw('o.id'),'desc')->paginate(15);
        }
        $order_status = config('app.order_status');
        return view('backstage/order/index',['list'=>$list,'order_status'=>$order_status]);
    }
    public function edit($id){
        $table = 'order';
        $data =['id'=>$id];
        $order = CommonModel::getOne($table,$data);
        $order_status = config('app.order_status');
        if($order->pid){
            $name = CommonModel::getField('product','name',['id'=>$order->pid]);
        }else{
            $name='商品已不存在';
        }
        $order->name = $name;
        return view('backstage/order/edit',['order'=>$order,'order_status'=>$order_status]);
    }
    public function doedit(Request $request){
        if($request->isMethod('POST')){
            $data = $request->all();
            $id = $data['id'];
            $order['status'] = intval($data['status']);
            $orderInfo = CommonModel::getOne('order',['id'=>$id]);
            $order_status = config('app.order_status');
            $moneyInfo = UserModel::getMoney($orderInfo->uid);
            if($orderInfo->status==0 && intval($data['status'])==2){
                return json_encode(['status' => 0, 'message'=>'操作有誤，沒有支付保證金不能直接完成交易']);
            }
            if(is_null($moneyInfo)){
                $account_money=0;
            }else{
                $account_money=$moneyInfo->account_money;
            }
            if(($orderInfo->money)>$account_money){
                return json_encode(['status' => 0, 'message'=>'用護余額不足']);
            }

            $save = CommonModel::updateData('order',['id'=>$id],$order);
            if($save == true){
                $logContent = "将订单状态由:<b style='color:red'>".$order_status[$orderInfo->status]."</b>,修改为:<b style='color:red'>".$order_status[$order['status']]."</b>。";
                saveCommonLog($id, $logContent, 'order', session('admin')->real_name, session('admin')->id, 5);
                if($order['status']==2){
                    CommonModel::updateData('order',['id'=>$id],['time_end'=>time() ]);
                }
                if($orderInfo->status==0 && intval($data['status'])==1){
                    userMoneyLog($orderInfo->uid, 1, $orderInfo->money, $data['id'],'支付保證金，待取貨');
                }
                $arrJson = array('status' => 1, 'message'=>'操作成功','jumpUrl'=>$this->redirectTo);
            }else{
                $arrJson = array('status' => 0, 'message'=>'操作失敗');
            }
            return json_encode($arrJson);
        }
    }


}
